package cn.huobi.pet.service.impl;

import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.basic.util.BaiduAuditUtils;
import cn.huobi.basic.util.FreemarkUtil;
import cn.huobi.pet.domain.Pet;
import cn.huobi.pet.domain.PetDetail;
import cn.huobi.pet.domain.PetOnlineAuditLog;
import cn.huobi.pet.mapper.PetDetailMapper;
import cn.huobi.pet.mapper.PetMapper;
import cn.huobi.pet.mapper.PetOnlineAuditLogMapper;
import cn.huobi.pet.service.IPetDetailService;
import cn.huobi.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huobi
 * @since 2022-10-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private IPetDetailService petDetailService;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Value("${pagedir}")
    private String path;

    /**
     * 添加
     * @param pet
     */
    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = null;
        if (pet.getDetail() == null ){
            petDetail = new PetDetail();
        }else {
            petDetail = pet.getDetail();
        }
        petDetail.setPetId(pet.getId());
        petDetailService.add(petDetail);
    }

    /**
     * 修改
     * @param pet
     */
    @Override
    public void update(Pet pet) {
        super.update(pet);
        Long id = pet.getId();
        PetDetail detail = pet.getDetail();
        detail.setPetId(id);
        petDetailMapper.updateByPetId(detail);
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {


        // 过滤已上架的，并把数据转为map
        Map<Long, Pet> petMap = petMapper.findAll().stream().filter(pet -> pet.getState() == 0)
                .collect(Collectors.toMap(Pet::getId, pet -> pet));
        Date date = new Date();
        for (Long id : ids) {
            Pet pet = petMap.get(id);
            StringBuilder stringBuilder = new StringBuilder();
            if (pet != null){
                if (!BaiduAuditUtils.TextCensor(pet.getName())){
                    stringBuilder.append("宠物上架百度AI审核名称失败，宠物名称为：" + pet.getName() + ";");

                }

                if (pet.getResources().split(",").length > 1){
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[0])){
                        stringBuilder.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");

                    }
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[1])){
                        stringBuilder.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");

                    }
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources().split(",")[2])){
                        stringBuilder.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");

                    }
                }else {
                    if (!BaiduAuditUtils.ImgCensor(pet.getResources())){
                        stringBuilder.append("宠物上架百度AI审核图片失败，宠物图片地址为：" + pet.getResources() + ";");

                    }
                }

                if (stringBuilder.length() > 0){
                    PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                    petOnlineAuditLog.setAuditTime(date);
                    petOnlineAuditLog.setPetId(id);
                    petOnlineAuditLog.setNote(stringBuilder.toString());
                    petOnlineAuditLog.setState(0);
                    petOnlineAuditLogMapper.add(petOnlineAuditLog);
                    continue;
                }
                pet.setState(1L);
                pet.setOnsaletime(date);
                petMapper.update(pet);

                Pet petModel = petMapper.getPetDetail(id);
                String model ="testFreemarker.ftl";
                FreemarkUtil.createFreemark(model,petModel,path,id);

            }
        }

    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            if (pet.getState() == 1){
                petMapper.batchOffsale(id);

                File file = new File("E:\\WorkSpace\\pethome-web\\pethome-web\\freemarker\\petDetail\\" + id + ".html");
                file.delete();

            }
        }
    }

    /**
     * 根据id查询宠物详情
     */
    @Override
    public Pet getPetDetail(Long id) {
        return petMapper.getPetDetail(id);
    }

}
